<?

header("Cache-control: private");
// all subdomains save in one session
$domain = array_reverse(explode('.', $_SERVER['HTTP_HOST']));
ini_set('session.cookie_domain', '.' . $domain[1] . '.' . $domain[0]);

if (substr(@$_GET['params'], 0, 5) == 'TOKEN')
    session_id(substr($_GET['params'], 5, strlen($_GET['params'])));
session_start();
define('TOKEN', session_id());
//echo TOKEN;
// debug
$debug;

//echo (print_r($_REQUEST,1));
// create empty $data object
$data = new stdClass();
@$data->bench->start = microtime(true);

// include configuration
include 'config.php';

$data->bench->configuration = microtime(true) - $data->bench->start;

// scan and include apps classes
foreach ($data->APPS as $i)
    foreach (glob('apps/' . $i . '/lib/*.php') as $j)
        include $j;

$data->bench->classes = microtime(true) - $data->bench->start;

// ACCEPT LANGUAGES / LOCALES  apps/home/lib/classes/classWorld.php
list($locale, $lang) = world::locale(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 5), (@$_GET['lang'] ? $_GET['lang'] : @$_SESSION['lang']));
define('LANG', @$_SESSION['lang'] = $lang);
define('LOCALE', $locale);
// link prefix if subdomain ? [domain + lang] : lang
define('PREFIX', count($domain[0] == 'blog') > 2 ? 'http://' . DOMAIN . '/' . LANG : '/' . LANG);
// supported language list
$data->LANGUAGES = world::languages();

$APP = (SETUP ? 'common' : (@$_GET['app'] ? $_GET['app'] : 'home'));
if (!in_array($APP, $data->APPS)) {
    $_GET['params'] = $APP;
    $APP = 'profile';
}

// set request data
define('FORMAT', (@$_GET['format'] ? $_GET['format'] : DEFFORMAT));
define('APP', $APP);
define('CMD', SETUP ? 'editorial,check' : (@$_GET['cmd'] ? $_GET['cmd'] : 'default'));
define('ADMINISTERIAL', strstr(CMD, ',', true) == 'administerial' ? 1 : 0);
define('EDITORIAL', strstr(CMD, ',', true) == 'editorial' ? 1 : 0);
define('PARAMS', @$_GET['params']);
define('THEME', ($_SESSION['theme'] = (@$_GET['theme'] ? $_GET['theme'] : (@$_SESSION['theme'] ? $_SESSION['theme'] : DEFTHEME))));

$data->bench->requests = microtime(true) - $data->bench->start;

// MULTI LANGUAGE with PHP_GETTEXT
setlocale(LC_ALL, LOCALE . '.UTF-8');
// bind language files
if ($data->APPS)
    foreach ($data->APPS as $i)
        bindtextdomain($i, './apps/' . $i . '/locale');

$data->bench->bindtextdomain = microtime(true) - $data->bench->start;
/* --------------------------------------------------------------
  START APP
  -------------------------------------------------------------- */
// set application local domain
textdomain(APP);
// open db connection
db::connect();
$data->bench->dbconnection = microtime(true) - $data->bench->start;
// scan and include apps default script
if (!SETUP)
    foreach ($data->APPS as $i)
        @include 'apps/' . $i . '/default.php';
// include the requested app file
include 'apps/' . APP . '/trunk/' . str_replace(',', '/', CMD) . '.php';
// close db connection
db::disconnect();
$data->bench->process = microtime(true) - $data->bench->start;
/* --------------------------------------------------------------
  DATA FORMATTING	(THEME & TEMPLATE)
  -------------------------------------------------------------- */
// format to data object
common::format($data);
?>